home *** CD-ROM | disk | FTP | other *** search
/ Delphi Informant Complete 1995 - 2000 / Delphi Informant Complete 1995 to 2000.iso / Delphi Informant Magazine Complete Works SOURCE CODE 1998.rar / 1998 / Dec / di9812me / PluginSample / 1 / shell1 / main.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-03-23  |  2.1 KB  |  97 lines

  1. unit main;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   Menus, StdCtrls, Buttons;
  8.  
  9. const
  10.     cPLUGIN_MASK    = '*.plg';
  11.  
  12. type
  13.   TfrmMain = class(TForm)
  14.     MainMenu1: TMainMenu;
  15.     File1: TMenuItem;
  16.     Exit1: TMenuItem;
  17.     GroupBox1: TGroupBox;
  18.     memPlugins: TMemo;
  19.     BitBtn1: TBitBtn;
  20.     procedure Exit1Click(Sender: TObject);
  21.     procedure FormCreate(Sender: TObject);
  22.   private
  23.     { Private declarations }
  24.     procedure LoadPlugins;
  25.     procedure LoadPlugin(sr: TSearchRec);
  26.   public
  27.     { Public declarations }
  28.   end;
  29.  
  30. var
  31.   frmMain: TfrmMain;
  32.  
  33. implementation
  34.  
  35. uses Common;
  36.  
  37. {$R *.DFM}
  38.  
  39. procedure TfrmMain.Exit1Click(Sender: TObject);
  40. begin
  41.     frmMain.Close;
  42. end;
  43.  
  44. {Iterate the application directory looking for plugin files}
  45. procedure TfrmMain.LoadPlugins;
  46. var sr: TSearchRec;
  47.     path: string;
  48.     Found: integer;
  49. begin
  50.     path := ExtractFilePath(Application.Exename);
  51.     try
  52.         Found := FindFirst(path+cPLUGIN_MASK, 0, sr);
  53.         while Found = 0 do
  54.         begin
  55.             LoadPlugin(sr);
  56.             Found := FindNext(sr);
  57.         end;
  58.     finally
  59.         FindClose(sr);
  60.     end;
  61. end;
  62.  
  63. {Load the specified plugin DLL}
  64. procedure TfrmMain.LoadPlugin(sr: TSearchRec);
  65. var Description: String;
  66.     LibHandle: integer;
  67.     DescribeProc: TPluginDescribe;
  68. begin
  69.     LibHandle := LoadLibrary(Pchar(sr.Name));
  70.     if LibHandle <> 0 then
  71.     begin
  72.         DescribeProc := GetProcAddress(LibHandle, cPLUGIN_DESCRIBE);
  73.         if assigned(DescribeProc) then
  74.         begin
  75.             DescribeProc(Description);
  76.             memPlugins.Lines.Add(Description);
  77.         end
  78.         else
  79.         begin
  80.             Messagedlg('File "'+sr.Name+'" is not a valid plugin.',
  81.                         mtInformation, [mbOK], 0);
  82.         end;
  83.     end
  84.     else
  85.     begin
  86.         Messagedlg('An error occurred loading the plugin "'+sr.Name+'".',
  87.                     mtError, [mbOK], 0);
  88.     end;
  89. end;
  90.  
  91. procedure TfrmMain.FormCreate(Sender: TObject);
  92. begin
  93.     LoadPlugins;
  94. end;
  95.  
  96. end.
  97.